clear all;

%% construct data
cd ..; cd ..; cd ..; cd ..; 
load('code_main_model\1_solve_ss\input\input.mat','Pi'); total_size=sum(Pi);
load('code_main_model\6_implied_w_r\computation_results\data.mat','frac_credit','frac_entre','labor_demand_stat','income_entre','income_worker','grid_income','pdf_B_forward','period_trans','nmkt');
load('code_main_model\6_implied_w_r\input\comm_loc_model.mat','comm_loc_model');
cd 'code_figure_table\maintext\Table 2\model';

income_dist=income_entre+income_worker;
income(1:period_trans-1,1:nmkt)=nan;
for t=1:period_trans-1
    for i=1:nmkt
        income(t,i)=dot(squeeze(income_dist(t,i,:)),grid_income)/sum(squeeze(income_dist(t,i,:)));
    end
end

mktsize=sum(pdf_B_forward,[3,4,5]);
mktsize=mktsize(1:period_trans-1,:);
frac_credit=frac_credit./mktsize;
frac_entre=frac_entre./mktsize;
employment=labor_demand_stat./mktsize;

load('DID_a_matching.mat','treatmkt','controlmkt'); % year, market index, income, worker, entre, credit
nmkt=size(treatmkt,1);
year=[1986, 1988, 1990, 1992, 1994, 1996];
nyear=length(year);
n=nmkt*nyear;

treat(1:n,1:6)=0;
for i_mkt=1:nmkt
    index=(i_mkt-1)*nyear+1:i_mkt*nyear;
    treat(index,1)=year;
    treat(index,2)=treatmkt(i_mkt);
    treat(index,3)=log(income(1:2:11,treatmkt(i_mkt))); % income per capita
    treat(index,4)=log(employment(1:2:11,treatmkt(i_mkt)).*mktsize(1:2:11,treatmkt(i_mkt))*total_size);
    treat(index,5)=frac_entre(1:2:11,treatmkt(i_mkt));
    treat(index,6)=frac_credit(1:2:11,treatmkt(i_mkt));
end

control(1:n,1:6)=0;
for i_mkt=1:nmkt
    index=(i_mkt-1)*nyear+1:i_mkt*nyear;
    control(index,1)=year;
    control(index,2)=controlmkt(i_mkt);
    control(index,3)=log(income(1:2:11,controlmkt(i_mkt)));
    control(index,4)=log(employment(1:2:11,controlmkt(i_mkt)).*mktsize(1:2:11,controlmkt(i_mkt))*total_size);
    control(index,5)=frac_entre(1:2:11,controlmkt(i_mkt));
    control(index,6)=frac_credit(1:2:11,controlmkt(i_mkt));
end

%% add year when bank opens
treat=[treat(:,1:2),nan(size(treat,1),2),treat(:,3:end)];
control=[control(:,1:2),nan(size(control,1),2),control(:,3:end)];

for i_mkt=1:nmkt 
    index=find(treat(:,2)==treatmkt(i_mkt));
    
    branch_year=find(comm_loc_model(treatmkt(i_mkt),:),1,'first');
    treat(index,3)=1984+branch_year;
end
treat(:,4)=treat(:,3)-treat(:,1);

%% add treated dummy and interactions
treated=ones(n,1);

p_m3=0; p_m3(1:n,1)=0;
p_m3(treat(:,4)>=4)=1;
p_m2=0; p_m2(1:n,1)=0;
p_m2(treat(:,4)==2 | treat(:,4)==3)=1;
p_m1=0; p_m1(1:n,1)=0;
p_m1(treat(:,4)==0 | treat(:,4)==1)=1;
p_0=0; p_0(1:n,1)=0;
p_0(treat(:,4)==-2 | treat(:,4)==-1)=1;
p_p1=0; p_p1(1:n,1)=0;
p_p1(treat(:,4)==-4 | treat(:,4)==-3)=1;
p_p2=0; p_p2(1:n,1)=0;
p_p2(treat(:,4)<=-5)=1;

p_m3xt=p_m3.*treated;
p_m2xt=p_m2.*treated;
p_m1xt=p_m1.*treated;
p_0xt=p_0.*treated;
p_p1xt=p_p1.*treated;
p_p2xt=p_p2.*treated;

treat=[treat, treated, p_m3, p_m2, p_m1, p_0, p_p1, p_p2, p_m3xt, p_m2xt, p_m1xt, p_0xt, p_p1xt, p_p2xt];
control=[control, zeros(n,1), p_m3, p_m2, p_m1, p_0, p_p1, p_p2, zeros(n,6)];

market_y=[treat;control];


%% save

filename = 'panel_all.xlsx';
market_y=array2table(market_y,'VariableNames',{'year','fixedeffectdummy','eventyear_index','gapyear_index','incpc','num_worker','entre','credit','treated','p_m3','p_m2','p_m1','p_0','p_p1','p_p2','p_m3xt','p_m2xt','p_m1xt','p_0xt','p_p1xt','p_p2xt'});
writetable(market_y,filename,'Sheet','Sheet1','WriteVariableNames',true);
